/*
 * RELIC is an Efficient LIbrary for Cryptography
 * Copyright (c) 2009 RELIC Authors
 *
 * This file is part of RELIC. RELIC is legal property of its developers,
 * whose names are not listed here. Please refer to the COPYRIGHT file
 * for contact information.
 *
 * RELIC is free software; you can redistribute it and/or modify it under the
 * terms of the version 2.1 (or later) of the GNU Lesser General Public License
 * as published by the Free Software Foundation; or version 2.0 of the Apache
 * License as published by the Apache Software Foundation. See the LICENSE files
 * for more details.
 *
 * RELIC is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the LICENSE files for more details.
 *
 * You should have received a copy of the GNU Lesser General Public or the
 * Apache License along with RELIC. If not, see <https://www.gnu.org/licenses/>
 * or <https://www.apache.org/licenses/>.
 */

/**
 * @file
 *
 * Tests for random number generation.
 *
 * @ingroup test
 */

#include <stdio.h>

#include "relic.h"
#include "relic_test.h"

#if RAND == HASHD

/*
 * Test vectors taken from:
 * - http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/Hash_DRBG.pdf
 */

#if MD_MAP == SH224

#define FUNCTION "sha-224"

uint8_t seed1[62];

uint8_t result1[] = {
	0x5E, 0x68, 0xBD, 0xE0, 0x9A, 0xAA, 0x08, 0xBC, 0x11, 0xB3,
	0x27, 0x90, 0x2C, 0x82, 0xF0, 0x11, 0x4C, 0xBA, 0x0F, 0x9C,
	0xCC, 0xA6, 0x20, 0x3B, 0xA3, 0x94, 0x00, 0x91, 0x3E, 0xCD,
	0x36, 0x71, 0xA5, 0xB6, 0x0E, 0xF9, 0x22, 0x99, 0x9D, 0x90,
	0xFC, 0xEE, 0xEC, 0x5C, 0x22, 0x7E, 0x5D, 0x25, 0xC5, 0x69,
	0x21, 0xEE, 0x57, 0x2E, 0xD4, 0x72, 0xDC, 0x05, 0x6F, 0xCB,
	0x35, 0xFF, 0x51, 0xD7, 0xD9, 0xFB, 0x72, 0xFD, 0x4F, 0xD1,
	0xB1, 0xD2, 0x46, 0x45, 0x1D, 0xB5, 0x6C, 0xD4, 0xF8, 0x89,
	0xE4, 0x32, 0xE3, 0x27, 0x3F, 0x9E, 0xD8, 0x2D, 0xE3, 0xEF,
	0x7C, 0xD2, 0x8B, 0x6A, 0x9C, 0x0F, 0x4D, 0x78, 0xE5, 0xC8,
	0x45, 0x1D, 0x36, 0x34, 0x0A, 0x2B, 0xD7, 0xE6, 0x9F, 0xAB,
	0x32, 0xEB
};

uint8_t result2[] = {
	0x3F, 0xE2, 0xAD, 0x85, 0x24, 0xCE, 0x60, 0xE7, 0xC2, 0x1C,
	0x38, 0xA1, 0xDA, 0xB0, 0x2F, 0x3C, 0x20, 0x50, 0x11, 0x82,
	0xF3, 0x89, 0xEE, 0x69, 0x9F, 0x03, 0xFD, 0x87, 0x79, 0xED,
	0x17, 0xC6, 0x5B, 0x87, 0xAC, 0xEE, 0xEB, 0xF1, 0xD1, 0x46,
	0xE7, 0xEE, 0x10, 0x6C, 0xEC, 0x89, 0x55, 0xEE, 0xAF, 0xC1,
	0x8A, 0xBB, 0xC5, 0x62, 0xA5, 0x66, 0x8B, 0xB4, 0x9B, 0x0B,
	0x9C, 0x2F, 0xC7, 0x01, 0x89, 0xB2, 0x4E, 0x02, 0x73, 0x59,
	0x54, 0x58, 0xCD, 0x78, 0x0F, 0xBF, 0xA5, 0xF2, 0x16, 0x12,
	0x24, 0x21, 0xB8, 0x0B, 0xF7, 0x73, 0xD7, 0x36, 0xE6, 0xE1,
	0x1D, 0xEB, 0xB4, 0x24, 0x77, 0xD6, 0x96, 0x68, 0xD2, 0xF9,
	0x40, 0xC6, 0x60, 0xF6, 0xA2, 0xC1, 0xC9, 0xB4, 0x17, 0x95,
	0x92, 0xE0
};

#elif MD_MAP == SH256

uint8_t seed1[63];

#define FUNCTION "sha-256"

uint8_t result1[] = {
	0x77, 0xE0, 0x5A, 0x0E, 0x7D, 0xC7, 0x8A, 0xB5, 0xD8, 0x93,
	0x4D, 0x5E, 0x93, 0xE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0x68, 0x1A, 0x46, 0xB2, 0xAA, 0x86,
	0x94, 0xA0, 0xFE, 0x4D, 0xEE, 0xA7, 0x20, 0x92, 0x7A, 0x84,
	0xEA, 0xAA, 0x98, 0x5E, 0x59, 0xC1, 0x9F, 0x8B, 0xE0, 0x98,
	0x4D, 0x8C, 0xBE, 0xF8, 0xC6, 0x9B, 0x75, 0x41, 0x67, 0x64,
	0x19, 0x46, 0xE0, 0x40, 0xEE, 0x20, 0x43, 0xE1, 0xCC, 0xB2,
	0x9D, 0xCF, 0x06, 0x3C, 0x0A, 0x50, 0x83, 0x0E, 0x42, 0x8E,
	0x6D, 0xCA, 0x26, 0x2E, 0xCD, 0x77, 0xC5, 0x42
};

#elif MD_MAP == SH384

#define FUNCTION "sha-384"

uint8_t seed1[123];

uint8_t result1[] = {
	0x04, 0xFF, 0x23, 0xAD, 0x15, 0xE7, 0x87, 0x90, 0xAD, 0xD3,
	0x6B, 0x43, 0x8B, 0xBC, 0x09, 0x7C, 0x7A, 0x11, 0x74, 0x7C,
	0xC2, 0xCC, 0xEE, 0xDE, 0x2C, 0x97, 0x8B, 0x23, 0xB3, 0xDC,
	0x63, 0xB7, 0x32, 0xC9, 0x53, 0x06, 0x1D, 0x77, 0x64, 0x99,
	0x0A, 0xBF, 0xEF, 0xC4, 0x7A, 0x58, 0x1B, 0x92, 0x1B, 0xC0,
	0x42, 0x8C, 0x4F, 0x12, 0x21, 0x24, 0x60, 0xE4, 0x06, 0xA0,
	0xF0, 0x65, 0x1E, 0x7F, 0x0C, 0xB9, 0xA9, 0x0A, 0xBF, 0xDB,
	0x07, 0xB5, 0x25, 0x56, 0x5C, 0x74, 0xF0, 0xAA, 0x08, 0x50,
	0x82, 0xF6, 0xCF, 0x21, 0x3A, 0xAF, 0xAD, 0x0C, 0x06, 0x46,
	0x89, 0x50, 0x78, 0xF1, 0xE1, 0xFE, 0x4F, 0x35, 0xB8, 0x5F,
	0x95, 0xDE, 0xE3, 0xE8, 0x73, 0x05, 0x49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0x8C, 0xF0
};

uint8_t result2[] = {
	0xAD, 0xF3, 0x6B, 0x2A, 0x4A, 0xAA, 0x78, 0xEC, 0x4B, 0xE7,
	0x50, 0xEA, 0x91, 0x08, 0xD7, 0xBC, 0x9A, 0x7B, 0x52, 0x0A,
	0x46, 0x0D, 0xA5, 0xD9, 0x12, 0xCC, 0xF8, 0x3C, 0x51, 0x48,
	0xFD, 0xD3, 0x45, 0x9E, 0xC5, 0x84, 0xF1, 0x29, 0xFD, 0x0C,
	0x03, 0x28, 0xD1, 0xEF, 0x0B, 0xBE, 0x73, 0x8B, 0xD4, 0x77,
	0xA2, 0x48, 0x05, 0xDE, 0xA3, 0xFD, 0xB2, 0x75, 0xF2, 0x9F,
	0xE8, 0xCA, 0xEF, 0xDB, 0x5D, 0x4F, 0xBF, 0xB6, 0x27, 0x93,
	0x98, 0x7B, 0x11, 0x8D, 0x9F, 0x97, 0xA1, 0x4E, 0x67, 0x82
};

#elif MD_MAP == B2S256

uint8_t seed1[63];

#define FUNCTION "blake2s-256"

uint8_t result1[] = {
	0xD5, 0x26, 0xBD, 0x3B, 0x1A, 0x08, 0x91, 0x25, 0xB1, 0x2D,
	0xFF, 0x63, 0x57, 0x33, 0x85, 0x11, 0x7A, 0x1E, 0xF3, 0x7E,
	0xE0, 0xAE, 0xCE, 0x77, 0xAC, 0x19, 0x8B, 0x92, 0x7A, 0xDE,
	0x79, 0x23, 0x0E, 0xA9, 0x25, 0xF3, 0x33, 0xB4, 0x17, 0xC7,
	0xF7, 0x0A, 0xEB, 0x25, 0x0C, 0x9B, 0xAF, 0x38, 0xD0, 0x5C,
	0xCE, 0xC6, 0xB5, 0x63, 0x70, 0x15, 0xD7, 0x36, 0x4A, 0x99,
	0x25, 0xE9, 0xE1, 0x76, 0x75, 0xE0, 0x1C, 0x8E, 0x1E, 0x0B,
	0xC1, 0xE2, 0x8F, 0xB7, 0xCA, 0x56, 0x69, 0xB7, 0xAB, 0x5B,
	0x45, 0x6F, 0x5B, 0xA4, 0xF3, 0xAA, 0x54, 0x30, 0x3B, 0x44,
	0xFF, 0x35, 0xBF, 0xBB, 0xD8, 0xA3, 0xEA, 0xFA, 0x32, 0x8F,
	0xD0, 0x68, 0x76, 0x5B, 0x47, 0x9E, 0x61, 0x73, 0xEB, 0xF8,
	0x84, 0x1D, 0xF5, 0xA7, 0xEF, 0x91, 0x47, 0x19, 0x30, 0x12,
	0xF6, 0x7C, 0x04, 0x7A, 0x4F, 0x4F, 0x07, 0xA0
};

uint8_t result2[] = {
	0xAB, 0x61, 0x58, 0xF2, 0x82, 0x88, 0xAA, 0x0A, 0xD1, 0xA3,
	0x98, 0xAD, 0x73, 0xD5, 0xD5, 0x1D, 0x6E, 0xDE, 0xA4, 0x24,
	0xA7, 0x76, 0x3D, 0x77, 0xD5, 0x33, 0xB8, 0x1B, 0xE7, 0x43,
	0x32, 0x0A, 0xB6, 0x44, 0x4D, 0x47, 0x54, 0x68, 0x69, 0x1B,
	0xA7, 0x5E, 0xA4, 0x90, 0x1D, 0x21, 0xBA, 0x3B, 0xC3, 0x7B,
	0x60, 0x3A, 0xDB, 0xD5, 0xDC, 0xCB, 0x8B, 0x6E, 0xB6, 0x6A,
	0xA0, 0xA7, 0x5B, 0xBB, 0x40, 0xC4, 0x7E, 0xB9, 0xD0, 0xC9,
	0xDB, 0xFF, 0xAC, 0x19, 0xFA, 0xB2, 0x71, 0x41, 0xAB, 0x0A,
	0x41, 0xEB, 0x9C, 0xD1, 0x42, 0x08, 0x97, 0xAF, 0x17, 0x87,
	0x45, 0x7A, 0x2C, 0x7D, 0x8F, 0x43, 0xB3, 0x85, 0x49, 0xFF,
	0x28, 0x81, 0xE9, 0x5C, 0x85, 0x58, 0x06, 0x74, 0x11, 0x3A,
	0xDC, 0x00, 0x29, 0xA0, 0x78, 0xC8, 0xA7, 0xE4, 0xEB, 0x2D,
	0x9D, 0x07, 0xE1, 0x64, 0xCC, 0x0B, 0x94, 0x37
};

#endif

static int test(void) {
	int i, len = 2 * RLC_MD_LEN, code = RLC_ERR;
	uint8_t out[2 * RLC_MD_LEN], seed2[(RLC_RAND_SIZE - 1) / 2], seed3[(RLC_RAND_SIZE - 1) / 2];

	for (i = 0; i < (RLC_RAND_SIZE - 1) / 2; i++) {
		seed1[i] = i;
		seed2[i] = 0x80 + i;
		seed3[i] = 0xC0 + i;
	}
	for (; i < sizeof(seed1); i++) {
		seed1[i] = 0x20 + (i - (RLC_RAND_SIZE - 1) / 2);
	}

	TEST_ONCE("hash-dbrg (" FUNCTION ") random generator is correct") {
		rand_clean();
		rand_seed(seed1, sizeof(seed1));
		rand_bytes(out, len);
		TEST_ASSERT(memcmp(out, result1, len) == 0, end);
		rand_bytes(out, len);
		TEST_ASSERT(memcmp(out, result1 + len, len) == 0, end);
	}
	TEST_END;

	TEST_ONCE("hash-dbrg (" FUNCTION ") reseeding is correct") {
		rand_clean();
		rand_seed(seed1, sizeof(seed1));
		rand_seed(seed2, sizeof(seed2));
		rand_bytes(out, len);
		TEST_ASSERT(memcmp(out, result2, len) == 0, end);
		rand_seed(seed3, sizeof(seed3));
		rand_bytes(out, len);
		TEST_ASSERT(memcmp(out, result2 + len, len) == 0, end);
	}
	TEST_END;

	code = RLC_OK;

  end:
	return code;
}

#elif RAND == UDEV

static int test(void) {
	uint8_t out[20], digit;

	TEST_ONCE("reading from /dev/urandom is correct") {
		digit = 0;
		memset(out, 0, sizeof(out));
		rand_bytes(out, sizeof(out));
		for (int j = 0; j < sizeof(20); j++) {
			digit ^= out[j];
		}
		TEST_ASSERT(digit != 0, end);
	}
	TEST_END;
  end:
	return RLC_OK;
}

#elif RAND == RDRND

static int test(void) {
	uint8_t out[64];
	int len = sizeof(out) / 2, code = RLC_ERR;

	TEST_ONCE("rdrand hardware generator is non-trivial") {
		memset(out, 0, 2 * len);
		rand_bytes(out, len);
		/* This fails with negligible probability. */
		TEST_ASSERT(memcmp(out, out + len, len) != 0, end);
	}
	TEST_END;

	code = RLC_OK;

  end:
	return code;
}

#elif RAND == CALL

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

static void test_bytes(uint8_t *buf, int size, void *args) {
	int c, l, fd = *(int *)args;

	if (fd == -1) {
		RLC_THROW(ERR_NO_FILE);
	}

	l = 0;
	do {
		c = read(fd, buf + l, size - l);
		l += c;
		if (c == -1) {
			RLC_THROW(ERR_NO_READ);
		}
	} while (l < size);
}

static int test(void) {
	uint8_t out[20], digit;
	int fd = open("/dev/urandom", O_RDONLY);

	TEST_ONCE("callback to reading /dev/urandom is correct") {
		digit = 0;
		memset(out, 0, sizeof(out));
		rand_bytes(out, sizeof(out));
		for (int j = 0; j < sizeof(20); j++) {
			digit ^= out[j];
		}
		TEST_ASSERT(digit != 0, end);
		rand_seed(&test_bytes, (void *)&fd);
		rand_bytes(out, sizeof(out));
		for (int j = 0; j < sizeof(20); j++) {
			digit ^= out[j];
		}
		TEST_ASSERT(digit != 0, end);
	}
	TEST_END;
  end:
  	close(fd);
	return RLC_OK;
}

#endif

int main(void) {
	if (core_init() != RLC_OK) {
		core_clean();
		return 1;
	}

	util_banner("Tests for the RAND module:\n", 0);

	if (test() != RLC_OK) {
		core_clean();
		return 1;
	}

	util_banner("All tests have passed.\n", 0);

	core_clean();
	return 0;
}
